package com.boss.salary.dao.impl;


import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalaryBackLogDao;
import com.boss.salary.entity.SalaryBackLog;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


@Repository
public class SalaryBackLogDaoImpl extends BaseDaoImpl implements ISalaryBackLogDao {
    @Override
    public void saveSalaryBackLog(String bankName, String bankCode, String log_type, String province) {
        StringBuilder sb = new StringBuilder();
        sb.append(" insert into ")
                .append(TableName.SALARY_BACK_LOG)
                .append(" (id, bank_code, bank_name, create_time, update_time, is_read, is_deleted, log_type, mof_div_code) ")
                .append(" values ");
                sb.append(" (?, ?, ?, ?, ?, ?, ?, ?, ?) ");

            Object[] params = new Object[]{
                    SysUtils.generateGuid(),bankCode,bankName, String.valueOf(DateUtils.getCurrentTimeStamp())
                    ,String.valueOf(DateUtils.getCurrentTimeStamp()),0,0,log_type,province
            };


        this.getJdbcTemplate().update(sb.toString(), params);
    }

    @Override
    public List<SalaryBackLog> getSalaryBackLogByProvince(String province) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id, bank_code, bank_name, create_time, update_time, is_read, is_deleted, log_type, mof_div_code ")
           .append(" from ").append(TableName.SALARY_BACK_LOG).append(" where mof_div_code=? and is_read=0 ");
        paramList.add(province);

        return this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper(SalaryBackLog.class), paramList.toArray());
    }

    @Override
    public void updateSalaryBackLog(String id) {
        StringBuilder sb = new StringBuilder();
        sb.append(" update ")
                .append(TableName.SALARY_BACK_LOG)
               .append(" set is_read=1, update_time=? where id=?");

        Object[] params = new Object[]{
               String.valueOf(DateUtils.getCurrentTimeStamp()),id
        };


        this.getJdbcTemplate().update(sb.toString(), params);
    }
}
